<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Kwhell
 * Date: 5/17/13
 * Time: 1:39 AM
 * To change this template use File | Settings | File Templates.
 */

require_once('cnnxn.php');

$user = $_POST['user'];
$pass = $_POST['pass'];

$logout = $_GET['logout'];

if ( !empty($logout) && $logout == 1 )
{
    Authentication::unAuthenticate();
}

$method = $_SERVER['REQUEST_METHOD'];

if ( $method == 'GET' && Authentication::isAuthenticated() )
{
    header('Location: https://'.$_SERVER['HTTP_HOST'].'/~phillipv/ava-player-database/dbca/index.php');
    exit();
}

if ( $method == 'POST' )
{
    if ( !empty($user) && !empty($pass) && Authentication::Authenticate($user,$pass) )
    {
        header('Location: https://'.$_SERVER['HTTP_HOST'].'/~phillipv/ava-player-database/dbca/index.php');
        exit();
    }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>AVA Stats Entry</title>
    <link type="text/css" rel="stylesheet" href="adminstyles.css" />
</head>
<body>
<form name="login" action="login.php" method="POST">

<?
if ( $method == 'GET' )
{
    echo 'Username: <input type="text" name="user" id="user" /><br>'.
         'Password: <input type="password" name="pass" id="pass" /><br>';
}
else
{
    $passwordMessage = empty($pass) ? 'Please enter a password' : '';
    $userMessage = empty($user) ? 'Please enter a username' : '';
    $authMessage = !empty($user) && !empty($pass) ? 'Incorrect username and/or password' : '';

    echo '<span>Username: </span><input type="text" name="user" id="user" /><span>'.$userMessage.'</span><br>'.
        '<span>Password: </span><input type="password" name="pass" id="pass" /><span>'.$passwordMessage.'</span><br>'.
        '<span>'.$authMessage.'</span>';
}

?>

<input type="submit" />
</form>
</body>
</html>